home *** CD-ROM | disk | FTP | other *** search
- Path: dfw.nkn.net!usenet
- From: hattan@fastlane.net (John Hattan)
- Newsgroups: comp.lang.c++
- Subject: Re: RANDOM NUMBER GENERATOR
- Date: Sun, 31 Dec 1995 22:18:41 GMT
- Organization: The Code Zone
- Message-ID: <4c725d$4h5@dfw.nkn.net>
- References: <4bphpa$rc6@useneta1.news.prodigy.com> <4bpvu9$2p5@alterdial.uu.net> <4bs8mq$lcc@useneta1.news.prodigy.com>
- NNTP-Posting-Host: nfw54.fastlane.net
- X-Newsreader: Forte Agent .99b.112
-
- BCNJ68B@prodigy.com (Tom Kellerman) wrote:
-
- >crowleyj@approach.com (John (Jay) wrote:
- >
- >>Here's a C++ class that generates good random numbers. I got the
- >>algorithm from MS Developer Network, which got it from a published
- >>article, which I can't seem to find now (probably Dr. Dobbs.). From
- >>what I remember, the article indicated that it has a theoretical
- >>repetition period in the 100's of trillions of iterations. Should be
- >>really easy to write your roll function using this.
- >>
- >> [random class carefully excised]
- >
- >Thank you very much for the Random Number Generator Class. I am only a
- >novice C++ programmer so I need some help in how to know use this class
- >to create a funcion that rolls two dice (return's a random number of 1-6,
- >adds it to another rendom number of 1-6). I need to create a funcion
- >rolldice() to do this. An example program of how I want to use this is to
- >roll 2 dice say 100 times and count how many 7's were rolled. I need help
- >in unsing that CLASS that you provided me to create my rolldice function.
-
- try this. For more complicated stuff, a random number class is a good
- idea (for example, to get a random number divisible by a value and/or
- contained within a bound). For something as simple as a die roll, just
- use the rand() and srand() functions. As far as I know, they should
- exist in every C++ you can find. . .
-
- main()
- {
- srand(time()); // seed the randomer so you
- // don't always get the same
- // stuff.
- int number=0;
- for (int i=1;i<=100;i++)
- {
- int result=rolldice();
- if (result==7) number=number+1;
- }
- cout<<"The number of 7's rolled out of 100 rolls is"<<number;
- }
-
- int rolldice(void)
- {
- // rand() returns a big random number, mod it to get the bound you
- // want.
- int d1 = (rand()%6)+1;
- int d2 = (rand()%6)+1;
- return d1+d2;
- }
-
- ---
- John Hattan High UberPopeness -The First Church of Shatnerology
- The Code Zone Sweet Software for a Saturnine World
- hattan@fastlane.net http://www.fastlane.net/~hattan/
-
-
-